package com.ishare.admin.controller;

import com.ishare.admin.service.SysMenuService;
import com.ishare.common.base.BaseController;
import com.ishare.common.entity.dto.BatchDto;
import com.ishare.common.entity.po.SysMenu;
import com.ishare.common.entity.vo.MenuTreeDataVO;
import com.ishare.common.entity.vo.MenuVO;
import com.ishare.common.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 运营平台系统菜单
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags = {"运营平台系统菜单"})
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;

    //@Autowired
    //private ShiroService shiroService;

    /**
     * 导航菜单
     */
    @GetMapping("/routes")
    @ApiOperation(value = "管理后台导航菜单",notes = "不同登录用户结果不同")
    public Wrapper getRoutesConfig() {
        List<MenuVO> menuList = null;
        if (getUser().getIsAdmin()==1){
            menuList = sysMenuService.getUserMenuList(null);
        }else {
            menuList = sysMenuService.getUserMenuList(getUserId());
        }

        return  Wrapper.ok(menuList);
    }

    /**
     * 所有菜单列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "管理后台菜单列表",notes = "超级管理员可看到全部列表")
    public Wrapper list() {
        List<MenuVO> menuVOS = null;
        if (getUser().getIsAdmin()==1){
            //管理员所有菜单
            menuVOS = sysMenuService.queryUserAllMenu(null);
        }else {
            menuVOS = sysMenuService.queryUserAllMenu(getUserId());
        }
        return Wrapper.ok(menuVOS);
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @GetMapping("/select")
    @ApiOperation(value = "选择菜单",notes = "")
    public Wrapper select() {
        List<MenuTreeDataVO> menuList = null;
        if (getUser().getIsAdmin()==1){
            //查询列表数据
            menuList = sysMenuService.select(null);
        }else {
            menuList = sysMenuService.select(getUserId());
        }
        return Wrapper.ok(menuList);
    }

    /**
     * 菜单信息
     */
    @GetMapping("/info/{menuId}")
    @ApiOperation(value = "菜单详情",notes = "")
    public Wrapper info(@PathVariable("menuId") Long menuId) {
        SysMenu menu = sysMenuService.getById(menuId);
        return Wrapper.ok(menu);
    }

    /**
     * 新增菜单
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增菜单",notes = "")
    public Wrapper save(@RequestBody SysMenu menu) {
        sysMenuService.save(menu);
        return Wrapper.ok();
    }

    /**
     * 修改菜单
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改菜单",notes = "")
    public Wrapper update(@RequestBody SysMenu menu) {
        menu.setUpdateTime(new Date());
        sysMenuService.updateById(menu);
        return Wrapper.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除菜单",notes = "")
    public Wrapper delete(@RequestBody BatchDto batchDto) {
        sysMenuService.deleteBatch(batchDto.getIds());

        return Wrapper.ok();
    }
}
